package com.demo1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(name="upload",urlPatterns="/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		//获取普通请求参数
		String name = request.getParameter("name");
		out.println("商品名称:"+name);
		//获取文件上传域
		Part part = request.getPart("pic");
		//获取文件上传的类型
		String contentType=part.getContentType();
		out.println("<br/>文件上传的类型:"+contentType);
		//获取文件上传的大小
		long size = part.getSize();
		out.println("<br/>文件上传的大小:"+size);
		//获取上传的文件表单组件的名称
		String fileName=part.getName();
		//获取上传提交的文件名
		String submittedFileName = part.getSubmittedFileName();
		out.println("<br/>文件名:"+fileName+",提交的文件名:"+submittedFileName);
		//将上传的文件写入服务器
		part.write(getServletContext().getRealPath("/uploadFiles/"+UUID.randomUUID()+".jpg"));
	}
}
